if($('._chargeType :checked').val() === "1" && !$('._mainJudge:eq(0) ._addJudge')[0]){
	show_msg({
		msg : '系统异常',
		time : 2000
	})
}
//添加套餐事件
$('._setMeal').click(function() {
	//当添加事件返回-1时，说明上一行内容没有完善，不能进行下一行的添加
	if(addJudge() == -1) {
		return
	}
	if($('._mainJudge:eq(0) ._addJudge').length <= 3) {	// 如果数据有4条就不能执行  //todo 这里改成4
		var removerarr = [];
		$('._mainJudge:eq(0) ._addJudge').each(function(index) {
			var thisValue = $(this).find('input')[0].value;
			removerarr.push(thisValue);
		});
		$('._mainJudge:eq(0)').append('<div class="_raw_error_param _addJudge"><div class="table-foot fl"><div class="tab" style="width:20%"><select class="kt5 _api_request_param" id="api_request_param_type"><option value="1个月" selected="selected">1个月</option><option value="3个月">3个月</option><option value="半年">半年</option><option value="一年">一年</option></select><input type="hidden" name="api_date" value="1个月" class="_api_request_param_type" /></div><div class="tab" style="width:20%"><input name="" mandatory="on" value="" class="kt5 _selling_price" autocomplete="off"  maxlength="50" style="width:80%" /><span class="package-danwei">元</span></div><div class="tab" style="width:20%"><input name="" mandatory="on" value="" class="kt5 _call_num" autocomplete="off"  maxlength="50" style="width:80%" /><span class="package-danwei">次</span></div><div class="tab" style="width:20%"><input name=""  value="" class="kt5 _return_num" autocomplete="off"  maxlength="50" style="width:80%" /><span class="package-danwei">条</span></div><div class="tab" style="width:19%"><input name=""  value="" class="kt5 _concurrence_num" autocomplete="off"  maxlength="50" style="width:70%" /><span class="package-danwei" style="width:22%">次/秒</span></div></div><div class="commentaryy "><a class="_delapps">删除</a></div></div>');
		var $_addJudge = $('._mainJudge:eq(0) ._addJudge select');
		var redom = $_addJudge[$_addJudge.length - 1];
		for(var i = 0; i < removerarr.length; i++) {
			try{
			redom.removeChild($(redom).find('[value=' + removerarr[i] + ']')[0]);	
			}catch(e){
				
			}			
		}
		$(redom).next('input').val($(redom).children().val());
		var removerarr = [];
		$('._mainJudge:eq(0) ._addJudge').each(function(index) {
			var thisValue = $(this).find('input')[0].value;
			removerarr.push(thisValue);
		});
		$_addJudge.each(function() {
			var vals = $(this).next('input').val();
			for(var i = 0; i < removerarr.length; i++) {
				if(vals !== removerarr[i]) {
					var doms = $(this).find('[value=' + removerarr[i] + ']')[0];
					doms && this.removeChild(doms);
				}
			}
		})

	}
	if($('._mainJudge:eq(0) ._addJudge:eq(0) > div:eq(1)').html() === '') {
		$('._mainJudge:eq(0) ._addJudge:eq(0) > div:eq(1)').html('<a class="_delapps">删除</a>');
	}
});

//获取所有的输入框DOM元素
var inputs = $('._addJudge').find('input')
	//是否添加事件   
function addJudge() {
	inputs = $('._mainJudge:eq(0) ._addJudge').find('input.kt5');
	var result = 1 //返回值，控制是否添加下一行元素
		//遍历，当有内容为空时添加样式，并返回-1
	inputs.each(function() {
		if(!this.value && this.getAttribute('mandatory')) {
			this.style.borderColor = '#ec6941'
			this.style.boxShadow = 'inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ce8483'
			$(this).blur(function() {
				if(this.value) {
					this.setAttribute('style', 'width:80%');
					$(this).unbind('blur');
				}
			})
			result = -1
		}
	})
	//console.log(result)
	return result
}

// 收费隐藏和显示
$('._chargeType').on('click', 'input', function() {
	switch(this.value) {
		case '0':
			$('._mainJudge').hide();
			$('._mainJudge_2').hide();
			break;
		case '1':
			//修改，更新API页面用到
			if ($('._mainJudge').length == 1) {
				$('._mainJudge').show();
				$('._mainJudge_2').show();
			}else{
				$('._mainJudge').eq(0).show();
				$('._mainJudge_2').eq(0).show();
			}
			break;
	}
})

// 获取数据
function getPackageData() {	
	var arr = [];
	$('._mainJudge:eq(0) ._addJudge').each(function(index) {
		var thisFinds = $(this).find('input');
		var package_date_A = 0;
		switch(thisFinds[0].value){
			case '1个月':
			package_date_A = 31;
			break;
			case '3个月':
			package_date_A = 92;
			break;
			case '半年':
			package_date_A = 183;
			break;
			case '一年':
			package_date_A = 365;
			break;
		}
		arr.push({
			'package_name': 'package_' + (+new Date())
			+ '_' + $('#api_name_en').val()
			+ '_' + parseInt(Math.random() * 10000)
			+ '_' + index,
			'package_cycle': thisFinds[0].value,
			'package_price': thisFinds[1].value,
			'limit_num': thisFinds[2].value,
			'return_num': thisFinds[3].value,
			'con_num': thisFinds[4].value,
			'package_date' : package_date_A
		});
	});
	arr.sort(function(d1, d2) {		
		return  (d1.package_price / d1.package_date)-(d2.package_price / d1.package_date);
	})
	return arr;
}

$(document)
	.on('click', '._delapps', function() {
		var vals = $(this).parent().parent('._raw_error_param').find('select').next('input').val();
		$(this).parent().parent('._raw_error_param').remove();
		var $_addJudge = $('._mainJudge:eq(0) ._addJudge select');
		$_addJudge.each(function() {	
			var str = $(this).next().val()
			var arr = [];
			$(this).children().each(function(index) {
				if(this.innerHTML!=str){
					arr.push('<option value="' + this.innerHTML + '">' + this.innerHTML + '</option>');	
				}else{
					arr.push('<option selected="selected" value="' + this.innerHTML + '">' + this.innerHTML + '</option>');
				}				
			})
			arr.push('<option value="' + vals + '">' + vals + '</option>');

			arr.sort(function(s1, s2) {
				var n1 = 0,
					n2 = 0;
				switch($(s1).val()) {
					case '1个月':
						n1 = 1;
						break;
					case '3个月':
						n1 = 3;
						break;
					case '半年':
						n1 = 6;
						break;
					case '一年':
						n1 = 12;
						break;
				}
				switch($(s2).val()) {
					case '1个月':
						n2 = 1;
						break;
					case '3个月':
						n2 = 3;
						break;
					case '半年':
						n2 = 6;
						break;
					case '一年':
						n2 = 12;
						break;
				}
				return n1 - n2;
			})

			this.innerHTML = arr.join('');
		});

		if($('._mainJudge:eq(0) ._addJudge').length === 1) {
			$('._mainJudge:eq(0) ._addJudge ._delapps').remove();
		}
	})
	.on('change', '._addJudge select', function() {
		//删除所有变化后的数据 添加所有变化前的数据,再给this添加变化后的数据
		var _this = this;
		var val = $(_this).val(); // 变化后的数据
		var nval = $(_this).next('input').val(); //变化前的数据
		if(val === nval) {
			return;
		}
		var $_addJudge = $('._addJudge select');
		$_addJudge.each(function() {
			$(this).children().each(function() {
				if(this.value === val && this.parentNode !== _this) {
					this.parentNode.removeChild(this); //删除所有变化后的数据
					return;
				}
			})
		});

		$_addJudge.each(function() {
			$(this).each(function() {
				if(this !== _this) {
					var arr = [];
					$(this).children().each(function(index) {
						arr.push('<option value="' + this.innerHTML + '"' + (index === 0 ? ' selected="selected"' : '') + '>' + this.innerHTML + '</option>');
					})
					arr.push('<option value="' + nval + '">' + nval + '</option>');
					arr.sort(function(s1, s2) {
						var n1 = 0,
							n2 = 0;
						switch($(s1).val()) {
							case '1个月':
								n1 = 1;
								break;
							case '3个月':
								n1 = 3;
								break;
							case '半年':
								n1 = 6;
								break;
							case '一年':
								n1 = 12;
								break;
						}
						switch($(s2).val()) {
							case '1个月':
								n2 = 1;
								break;
							case '3个月':
								n2 = 3;
								break;
							case '半年':
								n2 = 6;
								break;
							case '一年':
								n2 = 12;
								break;
						}
						return n1 - n2;
					})

					this.innerHTML = arr.join('');
				}
			})
		});
		$(_this).next('input').val(val);
	})

//收费--添加套餐判断
$('._mainJudge:eq(0)').on('blur','._addJudge div.tab input._selling_price',function(){
	if(!(/^\d+\.?\d{0,2}$/.test($(this).val()))){
		if(isNaN($(this).val())){

			$(this).css({'borderColor':'#ec6941','boxShadow':'inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ce8483'})
		}else if(!(/^\d+\.?\d{0,2}$/).test($(this).val())){
			$(this).val($(this).val().replace(/^(\d+\.?\d{0,2})\d*$/,'$1')) //当提现金额小数点后超过两位时，自动截取两位
		}
	}
	$(this).focus(function() {
		this.setAttribute('style', 'width:80%');
		$(this).unbind('blur');
	});
}).on('blur','._addJudge div.tab input._int_num',function() {
	if (!/^\d+$/.test(this.value)) {
		this.style.borderColor = '#ec6941'
		this.style.boxShadow = 'inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ce8483'
	}
	$(this).focus(function() {
		this.setAttribute('style', 'width:80%');
		$(this).unbind('blur');
	});
})